package com.catsic.web.controllers;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.controller.BaseController;
import com.catsic.mod.condition.ReceiveMessageCondition;
import com.catsic.mod.form.ReceiveMessageForm;
import com.catsic.mod.service.ReceiveMessageService;


@Controller
@RequestMapping("/receiveMessage")
public class ReceiveMessageController extends BaseController{

	@Autowired
	private ReceiveMessageService receiveMessageService;
	
	/**
	 * 查询收件箱
	 * @author cs
	 * @param condition
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value = "/query" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> query(ReceiveMessageCondition condition,Pageable pageable){
		Map<String,Object> result= new HashMap<String,Object>();
		result.put(CONTENT,receiveMessageService.findAll(condition, pageable));
		result.put(RESULT,RESULT_SUCCESS);
		return result;
	}
	
	/**
	 * 查询收件人类型
	 * @author cs
	 * @param code
	 * @return
	 */
	@RequestMapping(value = "/initAddresseeType")
	@ResponseBody
	public Map<String,Object> findAddresseeType(String code){
		Map<String,Object> result= new HashMap<String,Object>();
		result.put(CONTENT,receiveMessageService.findAddresseeType(code));
		result.put(RESULT,RESULT_SUCCESS);
		return result;
	}
	
	/**
	 * 根据收件人类型查询收件人
	 * @author cs
	 * @param condition
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value = "/searchAddressee" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> searchAddresseeByType(ReceiveMessageCondition condition,Pageable pageable){
		Map<String,Object> result= new HashMap<String,Object>();
		result.put(CONTENT,receiveMessageService.searchAddresseeByType(condition, pageable));
		result.put(RESULT,RESULT_SUCCESS);
		return result;
	}
	
	/**
	 * writeMessage:写信
	 * @author cs
	 * @param receiveMessageForm
	 * @return
	 */
	@RequestMapping(value = "/writeMessage" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> writeMessage(ReceiveMessageForm receiveMessageForm){
		receiveMessageService.add(receiveMessageForm);
		Map<String,Object> result= new HashMap<String,Object>();
		result.put(RESULT,RESULT_SUCCESS);
		return result;
	}
	
	/**
	 * backMessage:回信
	 * @author cs
	 * @param receiveMessageId
	 * @param UserName
	 * @param reContent
	 * @return
	 */
	@RequestMapping(value = "/backMessage" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> backMessage(String receiveMessageId,String UserName,String reContent){
		receiveMessageService.backMessage(receiveMessageId,UserName,reContent);
		Map<String,Object> result= new HashMap<String,Object>();
		result.put(RESULT,RESULT_SUCCESS);
		return result;
	}
	
	/**
	 * delete:删除信息
	 * @author cs
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String,Object> delete(String id){
		receiveMessageService.delete(id);
		Map<String,Object> result= new HashMap<String,Object>();
		result.put(RESULT, RESULT_SUCCESS);
		return result;
	}	
	
	@RequestMapping(value = "/note" , method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> findNote(String userId){
		Integer form=receiveMessageService.findCountById(userId);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(CONTENT, form);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}

}
